package crp.core.workflow.domain.expression.candidate;

import com.googlecode.aviator.AviatorEvaluatorInstance;
import com.qwlabs.cdi.Dispatchable;
import crp.core.shared.expression.ExpressionInstanceInitializer;
import crp.core.workflow.domain.expression.WorkflowExpressions;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

@ApplicationScoped
public class WorkflowCandidateExpressionInstanceInitializer
        implements Dispatchable<String>, ExpressionInstanceInitializer {
    private final WorkflowCandidateSubmitterFunction submitterFunction;
    private final WorkflowCandidateAnyPermissionsFunction anyPermissionsFunction;

    @Inject
    public WorkflowCandidateExpressionInstanceInitializer(WorkflowCandidateSubmitterFunction submitterFunction,
                                                          WorkflowCandidateAnyPermissionsFunction anyPermissionsFunction) {
        this.submitterFunction = submitterFunction;
        this.anyPermissionsFunction = anyPermissionsFunction;
    }

    @Override
    public boolean dispatchable(String context) {
        return context.startsWith(WorkflowExpressions.candidateTopic(null));
    }

    @Override
    public void initialize(AviatorEvaluatorInstance instance) {
        instance.addFunction(submitterFunction);
        instance.addFunction(anyPermissionsFunction);
    }
}
